package com.hfjyz.registration.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.registration.command.StatusSearchCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Apply;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.model.User;
import com.hfjyz.registration.service.RegistService;

public class StatusSearchController extends SimpleFormController {
	private RegistService registService;

	/**
	 * @return the registService
	 */
	public RegistService getRegistService() {
		return registService;
	}

	/**
	 * @param registService
	 *            the registService to set
	 */
	public void setRegistService(RegistService registService) {
		this.registService = registService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		StatusSearchCommand cmd = (StatusSearchCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		String submitType = cmd.getSubmitType();
		if ("search".equals(submitType)) {
			registService
			.loggin(request, ExamContants.LOGING_TYPE_INFO, (Integer) null,
					ExamContants.LOGING_USER_TYPE_USER,
					cmd.getIdCardNo(),
					ExamContants.LOGING_OP_TYPE_SEARCH, "审核状态查询");
			cmd.setShowDetail(false);
			if (!errors.hasErrors()) {
				registService.searchStatus(cmd, errors);
			}
			if (errors.hasErrors()) {
				return new ModelAndView(getFormView(), errors.getModel());
			}
			HttpSession session = request.getSession();
			session.setAttribute("UserInfo", cmd.getUser());
			session.setAttribute("ApplyInfo", cmd.getApply());
			cmd.setShowDetail(true);
			request.getSession().setAttribute("photoData" + cmd.getIdCardNo(),
					cmd.getUser().getPhoto());
			return new ModelAndView(getFormView(), errors.getModel());
		} else {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("UserInfo");
			Apply apply = (Apply) session.getAttribute("ApplyInfo");
			String sTrainingExp = user.getTrainingExp();
			if (sTrainingExp.length() > ExamContants.MAX_LENTH_FORPRINTDISPLAY) {
				user.setTrainingExp(sTrainingExp.substring(0,
						ExamContants.MAX_LENTH_FORPRINTDISPLAY - 2) + "...");
			}
			String sWorkExp = user.getWorkExp();
			if (sWorkExp.length() > ExamContants.MAX_LENTH_FORPRINTDISPLAY) {
				user.setWorkExp(sWorkExp.substring(0,
						ExamContants.MAX_LENTH_FORPRINTDISPLAY - 2) + "...");
			}
			String sSocialRel = user.getSocialRel();
			if (sSocialRel.length() > ExamContants.MAX_LENTH_FORPRINTDISPLAY) {
				user.setSocialRel(sSocialRel.substring(0,
						ExamContants.MAX_LENTH_FORPRINTDISPLAY - 2) + "...");
			}
			cmd.setUser(user);
			cmd.setApply(apply);
			registService
			.loggin(request, ExamContants.LOGING_TYPE_INFO, (Integer) null,
					ExamContants.LOGING_USER_TYPE_USER,
					cmd.getIdCardNo(),
					ExamContants.LOGING_OP_TYPE_SEARCH, "审核状态打印");
			registService.printApply(user.getId(), exam.getId());

			return new ModelAndView(getSuccessView(), errors.getModel());
		}
	}
}
